﻿#include <opencv2\opencv.hpp>  
#include <iostream>  
#include <fstream>
#include <string>  
#include<math.h>
using namespace cv;  
using namespace std;
const int width=160;
const int height=100;
const int frame=6;
double number=21;//展示图片的数量
int start[2]={-10,0};//初始偏移s
int offset[2]={1,0};//偏移
double data1[frame][height][width],data2[frame][height][width],data3[frame][height][width];
double data4[frame][height][width],data5[frame][height][width],data6[frame][height][width];


void main()    
{   
    ifstream in;
    in.open("g.txt");
     int colors[256]={0};
     IplImage *src1,*src2,*src3,*src4,*src5;     
	src1 = cvCreateImage(cvSize(width,height), IPL_DEPTH_8U,3);
	src2 = cvCreateImage(cvSize(width,height), IPL_DEPTH_8U,3);
	src3 = cvCreateImage(cvSize(width,height), IPL_DEPTH_8U,3);
	src4 = cvCreateImage(cvSize(width,height), IPL_DEPTH_8U,3);
	src5 = cvCreateImage(cvSize(width,height), IPL_DEPTH_8U,3);
	//src6 = cvCreateImage(cvSize(width,height), IPL_DEPTH_8U,3);

	int widthstep=src1->widthStep;
	uchar* a1=(uchar*)src1->imageData;
	uchar* a2=(uchar*)src2->imageData;
	uchar* a3=(uchar*)src3->imageData;
	uchar* a4=(uchar*)src4->imageData;
	uchar* a5=(uchar*)src5->imageData;
//	uchar* a6=(uchar*)src6->imageData;
	
	cout<<"initial"<<endl;
	for(int i=0;i<height;i++)
	{
	    for(int j=0;j<width;j++)
	    {
		   for(int k=0;k<frame;k++)
		   {
			  in>>data1[k][i][j];
			  in>>data2[k][i][j];
			  in>>data3[k][i][j];
		   }
		   //cout<<data1[i][j]<<" "<<data2[i][j]<<" "<<data3[i][j]<<endl;
	    }
	}
	//double data4[100][160],data5[100][160],data6[100][160];
	for(int i=0;i<height;i++)
	{
	    for(int j=0;j<width;j++)
	    {
		   for(int k=0;k<frame;k++)
		   {
			  data4[k][height-1-i][j]=data1[k][i][j];
			  data5[k][height-1-i][j]=data2[k][i][j];
			  data6[k][height-1-i][j]=data3[k][i][j];
		   }
		   //cout<<data1[i][j]<<" "<<data2[i][j]<<" "<<data3[i][j]<<endl;
	    }
	}
	for(int i=0;i<height;i++)
	{
	    for(int j=0;j<width;j++)
	    {
		   for(int k=0;k<frame;k++)
		   {
			  data1[k][i][j]=data4[k][i][j];
			  data2[k][i][j]=data5[k][i][j];
			  data3[k][i][j]=data6[k][i][j];
		   }
		   //cout<<data1[i][j]<<" "<<data2[i][j]<<" "<<data3[i][j]<<endl;
	    }
	}
	cout<<"ok"<<endl;
	for(int i=0;i<height;i++)
	{
	    for(int j=0;j<width;j++)
	    {
		  a1[ i*widthstep + j*src1->nChannels + 0]=(int)((data1[0][i][j])*255.0);
		  a1[ i*widthstep + j*src1->nChannels + 1]=(int)((data1[0][i][j])*255.0);
		  a1[ i*widthstep + j*src1->nChannels + 2]=(int)((data1[0][i][j])*255.0);

		  a2[ i*widthstep + j*src1->nChannels + 0]=(int)((data2[0][i][j])*255.0);
		  a2[ i*widthstep + j*src1->nChannels + 1]=(int)((data2[0][i][j])*255.0);
		  a2[ i*widthstep + j*src1->nChannels + 2]=(int)((data2[0][i][j])*255.0);

		  a3[ i*widthstep + j*src1->nChannels + 0]=(int)((data3[0][i][j])*255.0);
		  a3[ i*widthstep + j*src1->nChannels + 1]=(int)((data3[0][i][j])*255.0);
		  a3[ i*widthstep + j*src1->nChannels + 2]=(int)((data3[0][i][j])*255.0);
		  double color=0;
		  for(int k=0;k<frame;k++)
		  {
			 color+=data1[k][i][j]*data2[k][i][j]*data3[k][i][j];
		  }
		  color*=255;
		  if(color>300)
			 cout<<"error";
		  else if(color>255)
			 color=255;
		  a4[ i*widthstep + j*src1->nChannels + 0]=(int)color;
		  a4[ i*widthstep + j*src1->nChannels + 1]=(int)color;
		  a4[ i*widthstep + j*src1->nChannels + 2]=(int)color;

	    }
	}
	int i2=0;
	int j2=0;
	int i3=2*i2;
	int j3=2*j2;
	for(int time=0;time<number;time++)
	{
	    i2=start[0]+time*offset[0];
	    j2=start[1]+time*offset[1];
	    i3=2*i2;
	    j3=2*j2;
	    for(int i=0;i<height;i++)
	    {
		   for(int j=0;j<width;j++)
		   {
			  double color=0;
			 if((i+i3)<height&&(j+j3)<width&&(i+i3)>=0&&(j+j3)>=0)
			 {
				for(int k=0;k<frame;k++)
				{
				    color+=(data1[k][i][j]*data2[k][i+i2][j+j2]*data3[k][i+i3][j+j3]);
				}
			 }
			 else if((i+i2)<height&&(j+j2)<width&&(i+i2)>=0&&(j+j2)>=0)
			 {
				for(int k=0;k<frame;k++)
				{
				    color+=(data1[k][i][j]*data2[k][i+i2][j+j2]);
				}
			 }
			 else
			 {
				for(int k=0;k<frame;k++)
				{
				    color+=data1[k][i][j];
				}
			 }
			 color=color*255;
			  
			 if(color>255)
				color=255;
			 a5[ i*widthstep + j*src1->nChannels + 0]=(int)color;
			 a5[ i*widthstep + j*src1->nChannels + 1]=(int)color;
			 a5[ i*widthstep + j*src1->nChannels + 2]=(int)color;
		   }
	    }
	    cout<<i2<<" "<<i3<<" "<<j2<<" "<<j3<<endl;
	    char* c=new char[10];
	    itoa(time,c,10);
	    string d=c;
	    //cout<<(d+"offset.jpg");
	    cvSaveImage((d+"offset.jpg").data(),src5);
	    cvNamedWindow((d+"offset").data(),1);  
	    cvShowImage((d+"offset").data(),src5);  
	}
	in.close();
	cvSaveImage("1.jpg",src1);
	cvNamedWindow("1",1);  
     cvShowImage("1",src1);  
	cvSaveImage("2.jpg",src2);
	cvNamedWindow("2",1);  
     cvShowImage("2",src2);
	cvSaveImage("3.jpg",src3);
	cvNamedWindow("3",1);  
     cvShowImage("3",src3);  
	cvSaveImage("4.jpg",src4);
	cvNamedWindow("4",1);  
     cvShowImage("4",src4);  

     cvWaitKey(0);    
	cvDestroyWindow("1");
	cvDestroyWindow("2");
	cvDestroyWindow("3");
	cvDestroyWindow("4");
	//cvDestroyWindow("5");
     cvReleaseImage(&src1); 
	cvReleaseImage(&src2); 
	cvReleaseImage(&src3); 
	cvReleaseImage(&src4); 
	//cvReleaseImage(&src5); 
}
